﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcEngine.Mvc.StarterSite.Controllers;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.ViewData;
using MvcEngine.Core;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Helpers;
using MvcEngine.Blog.Helper;
using System.Web;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core.Configuration;
using MvcEngine.Blog.Components.Attributes;
using MvcEngine.Mvc;
using MvcEngine.Mvc.Models;
using System.IO;
using MvcEngine.Core.Extensions;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Content;
using MvcEngine.Blog.Models;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;

namespace MvcEngine.Blog.StarterSite.Controllers
{
    public class BlogOverviewController : SubController
    {
        public MvcEngineModel Overview(int ctrId)
        {
            var blogOverview = ContentManager.Current.GetPageContent(ctrId);
            OverviewPageContent content = new OverviewPageContent(blogOverview);

            var result = new List<BlogPost>();

            foreach (var item in content.BlogIds)
                result.AddRange(BlogPostManager.Current.GetPosts(item, content.PostCount));
            result.OrderBy(p => p.PublishedDate);

            var posts = new List<BlogOverviewViewModel>();
            foreach (var item in result.Take(content.PostCount))
            {
                var model = new BlogOverviewViewModel()
                {
                    Id = item.Id,
                    Slug = item.Slug,
                    BlogId = item.BlogId,
                    PostCategories = BlogCategoryManager.Current.GetAllByPost(item.Id),
                    PostTags = BlogPostTagManager.Current.GetAllByPost(item.Id),
                    Title = item.Title,
                    PublishedDate = item.PublishedDate.Value,
                    PostContent = item.PostContent
                };
                posts.Add(model);
            }

            return new MvcEngineModelList<BlogOverviewViewModel>()
            {
                List = posts,
                ViewName = "Overview"
            };
        }
    }
}
